package clean_handler

import (
	"file-cleaner/internal/file_cleaner/cleaner"
	v1log "github.com/ClessLi/component-base/pkg/log/v1"
	"os"
)

type RollbackHandlerImpl struct {
}

func (r RollbackHandlerImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	err := os.Link(info.SourceFileInfo().Path()+"."+backupSuffix, info.SourceFileInfo().Path())
	if err != nil {
		v1log.Error(err.Error())
		return false
	}
	v1log.Infof("file: %v rolled back from %v", info.SourceFileInfo().Path(), info.SourceFileInfo().Path()+"."+backupSuffix)
	return true
}

type FailedRecordHandlerImpl struct {
	collector *CleanTaskInfoCollector
}

func NewFailedRecordHandler(collector *CleanTaskInfoCollector) CleanHandler {
	handler := &FailedRecordHandlerImpl{collector: collector}
	return CleanHandler(handler)
}

func (f *FailedRecordHandlerImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	f.collector.Record(info, false)
	return true
}
